package com.orangelife.mobile.myhouse.activity;

import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.curry.android.http.OrangeHttpBiz;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.orangelife.mobile.R;
import com.markupartist.android.widget.PullToRefreshListView;
import com.orangelife.mobile.app.application.CurryApplication;
import com.orangelife.mobile.common.activity.PullRefreshBaseActivity;
import com.orangelife.mobile.common.view.EmptyLayout;
import com.orangelife.mobile.myhouse.adapter.HouseWaitConfirmLvAdapter;
import com.orangelife.mobile.myhouse.biz.HouseHttpBiz;
import com.orangelife.mobile.myhouse.entity.HouseBean;
import com.orangelife.mobile.myhouse.entity.WaitCertEntity;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 待认证、已过期列表
 * <p/>
 * Created by 吴格非 on 2015/11/12.
 */
public class HouseWaitConfirmActivity extends PullRefreshBaseActivity {

    @Bind(R.id.plvWaitConfirm)
    PullToRefreshListView plvList;
    @Bind(R.id.tvWaitConfirm)
    TextView tvWaitConfirm;
    @Bind(R.id.tvExpire)
    TextView tvExpire;
    @Bind(R.id.emptyLayout)
    EmptyLayout emptyLayout;

    private List<WaitCertEntity> mList = new ArrayList<>();

    private HouseWaitConfirmLvAdapter adapter;

    private String currentStatus = HouseBean.STATUS_WAIT;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_house_wait_confirm);
        ButterKnife.bind(this);
        setAdapter();
        setListener();
        setTabView();
        initTitle();
        setXListViewAndEmptyLayoutListener(plvList, emptyLayout, adapter);
    }

    @Override
    protected EmptyLayout getEmptyLayout() {
        return emptyLayout;
    }

    @Override
    protected List getData() {
        return mList;
    }

    @Override
    protected int getDataCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    protected void requestWhenPullRefresh() {
        requestList();
    }


    private void setAdapter() {
        adapter = new HouseWaitConfirmLvAdapter(this);
        plvList.setAdapter(adapter);
    }

    @OnClick({R.id.tvExpire, R.id.tvWaitConfirm})
    public void onClick(View view) {
        tab(view.getId());
    }

    /** 切换排序类型，最新商家或人气最高 */
    private void tab(int clickID) {
        currentReqMode = REQ_MODE_INIT;
        plvList.setSelection(0);
        showProgress();
        if (clickID == R.id.tvWaitConfirm) {
            currentStatus = HouseBean.STATUS_WAIT;
        } else {
            currentStatus = HouseBean.STATUS_EXPIRE;
        }
        adapter.setCurrentStatus(currentStatus);
        setTabView();
        requestList();
    }

    private void setTabView() {
        if (currentStatus == HouseBean.STATUS_WAIT) {
            tvExpire.setTextColor(getResources().getColor(R.color.text_black));
            tvWaitConfirm.setTextColor(getResources().getColor(R.color.title_orange));
            tvWaitConfirm.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
            tvExpire.getPaint().setFlags(0);
        } else {
            tvWaitConfirm.setTextColor(getResources().getColor(R.color.text_black));
            tvExpire.setTextColor(getResources().getColor(R.color.title_orange));
            tvExpire.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
            tvWaitConfirm.getPaint().setFlags(0);
        }
    }

    private void setListener() {
        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                requestList();
            }
        });
    }


    private void requestList() {
        HouseHttpBiz.requestWaitConfirmList(new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                dismissProgress();
                if (response != null) {
                    List<WaitCertEntity> listLoaded = HouseHttpBiz.handleWaitConfirm(response);
                    handleAfterRequest(isConnected(), OrangeHttpBiz.getTotalNum(response), listLoaded, plvList, adapter);
                }
            }
        }, pageSize, pageNo, currentStatus);
    }

    @Bind(R.id.ib_title_left)
    LinearLayout ibTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;

    @OnClick({R.id.ib_title_left})
    public void onClickTitle(View view) {
        switch (view.getId()) {
            case R.id.ib_title_left:
                CurryApplication.getInstance().finishActivity(this);
                break;

        }
    }

    private void initTitle() {
        tvTitle.setText("身份认证");
    }


}
